package com.mobiletrain.org.motherkitch.sliding;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.exception.DbException;
import com.mobiletrain.org.motherkitch.MyApplication;
import com.mobiletrain.org.motherkitch.R;
import com.mobiletrain.org.motherkitch.sliding.caipu.Caipu;
import com.mobiletrain.org.motherkitch.sliding.caipu.MyCaipuAdapter;

import java.util.ArrayList;
import java.util.List;

public class MyProductionActivity extends ActionBarActivity {

    private Toolbar mToolbar;
    private ListView listView;
    private MyCaipuAdapter myCaipuAdapter;
    private List<Caipu> all;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_production);
        initBar();
        listView = (ListView) findViewById(R.id.myprod_list);
        initData();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent i=new Intent();
                i.setClass(MyProductionActivity.this,CaipuDetaiActivity.class);
                i.putExtra("bean",all.get(position));
                startActivity(i);
            }
        });
    }


    public void initBar() {
        mToolbar = (Toolbar) findViewById(R.id.mypro_title);
        mToolbar.setNavigationIcon(R.drawable.back);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    public void initData() {
        DbUtils dbUtils = ((MyApplication) getApplication()).getDbUtils();
        all = null;
        try {
            all = dbUtils.findAll(Caipu.class);
        } catch (DbException e) {
            e.printStackTrace();
        }

        myCaipuAdapter = new MyCaipuAdapter(MyProductionActivity.this, all);
        listView.setAdapter(myCaipuAdapter);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_my_production, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
